﻿
$.extend({
    msg: {
        title: '系统提示',
        msgtemplate: "<div style='margin-top:10px;text-align:left;'>$msg$</div>",

        info: function (msg, fn) {
            var d = $.msg.msgtemplate.replace('$msg$', msg);
            $.messager.alert($.msg.title, d, 'info', fn);
        },
        warn: function (msg, fn) {
            var d = $.msg.msgtemplate.replace('$msg$', msg);
            $.messager.alert($.msg.title, d, 'warning', fn);
        },
        error: function (msg, fn) {
            var d = $.msg.msgtemplate.replace('$msg$', msg);
            $.messager.alert($.msg.title, d, 'error', fn);
        },
        question: function (msg, fn) {
            var d = $.msg.msgtemplate.replace('$msg$', msg);
            $.messager.alert($.msg.title, d, 'question', fn);
        },
        confirm: function (msg, fn) {
            var d = $.msg.msgtemplate.replace('$msg$', msg);
            $.messager.confirm($.msg.title, d, fn);
        },
        prompt: function (msg, fn) {
            var d = $.msg.msgtemplate.replace('$msg$', msg);
            $.messager.prompt($.msg.title, d, fn);
        },
        progress: $.messager.progress,
        //在当前窗体显式一个提示框，然后在一定时间内自动消失
        //args.text，要提示的文本
        //args.duration，提示框显式的时长（毫秒），默认为1500毫秒
        //args.onclose，关闭提示信息时的回调函数
        lightip: function (args) {
            if (!args) { args = {}; }
            var tipid = '_lightip';
            var tip;
            if ($.getEl(tipid)) {
                tip = $('#' + tipid);
            } else {
                var css = "position:absolute; color:#F1F1F1; font-style:oblique; word-spacing:5px; text-align:center; border:dotted 1px #FF99CC; min-width:200px; padding:3px 36px 3px 36px; background-color:Orange; font-size:14px; z-index:1;";
                tip = $('<div id="' + tipid + '" style="' + css + '"></div>').appendTo('body');
            }
            tip.html(args.text ? args.text : '提示文本');
            tip.css('top', 60);
            tip.css('left', ($(window).width() - tip.width()) / 2);
            tip.fadeIn('slow');
            //setTimeout('$("#' + tipid + '").fadeOut("slow")', args.duration ? args.duration : 1500);
            setTimeout(function () {
                $('#' + tipid).fadeOut('slow');
                if (args.onclose) {
                    args.onclose.call(this);
                }
            }, args.duration ? args.duration : 1500);
        }
    },

    /*
    title: '窗口标题',
    winId: ID（默认为'ylDialog'）,
    title: 窗口标题（默认为'title'）,
    refresh: 显示刷新（默认是根据url来判断是否执行刷新）,
    url: '所打开的链接地址'（默认为undefined）,
    data: 打开窗体时传入的参数对象（将转换成url参数）
    cWidth: 减少宽度值（默认为300）,
    cHeight: 减少高度值（默认为120）,
    width: 优先采用固定宽度，
    height: 优先采用固定高度,
    onclose: 回调函数，当关闭弹出窗体时调用，回调函数的参数由弹出页面自行传入
    */
    showWindow: function (options) {
        var defaults = { cWidth: 300, cHeight: 120, title: 'title', multiple: true, winId: '__ylwin' };
        var params = $.extend({}, defaults, options);
        if (!params.width) {
            params.width = $(document).width() - params.cWidth;
        }
        if (!params.height) {
            params.height = $(document).height() - params.cHeight;
        }
        var data = options.data ? options.data : {};
        data.t = Math.random();
        var url = $.toUrlParam(data, params.url);
        var ifrmID = params.winId + '_frm';
        var ifrm = null;

        var win = $('#' + params.winId);
        var content = '<iframe src="' + url + '" id="' + ifrmID + '" scrolling="auto" frameborder="0" style="width:100%;height:100%;"></iframe>';
       if (win.length == 0) {
            win = $('<div id="' + params.winId + '" style="overflow:hidden;"></div>');
            win.window({
                title: params.title,
                collapsible: true,
                minimizable: false,
                maximizable: true,
                resizable: true,
                modal: true,
                closed: false,
                width: params.width,
                height: params.height,
                content: content,
                onBeforeOpen: function () {
                    if (params.title == 'title') {
                        $($.getEl(ifrmID).contentWindow).bind('load', function () {
                            var title = $.getEl(ifrmID).contentWindow.document.title;
                            win.window('setTitle', title);
                        });
                    }
                },
                onClose: function () {
                    if (options.onClose) {
                        options.onClose($.getEl(ifrmID).contentWindow.closeArgs);
                    }
                   // winCloseFn($.getEl(ifrmID).contentWindow.closeArgs);
                    //                    var fn = $('#' + ifrmID).data('closeFn');
                    //                    if (fn) {
                    //                        fn($.getEl(ifrmID).contentWindow.closeArgs);
                    //                    }
                }
            });
            ifrm = $('#' + ifrmID);
            ifrm[0].initial = function () {
                var title = ifrm[0].contentWindow.document.title;
                win.window('setTitle', title);
            };
            ifrm.bind('load', ifrm.initial);
        } else {
            win.window('setTitle', params.title);
            if (params.refresh || win.window('options').url != url) {
                var doc = $.getEl(ifrmID).contentWindow.document;
                doc.write('');
                doc.close();
                $.getEl(ifrmID).src = url;
            }
            win.window('open');
        }
        $('#' + ifrmID).data('closeFn', params.onclose);
        $.getEl(ifrmID).contentWindow.id = params.winId;

        if (params.tag != null) {
            $.getEl(ifrmID).tag = params.tag;
        }
        return win;
    },

    //根据窗体ID关闭弹出窗体
    closeWindow: function (winId) {
        if (!winId) {
            winId = '__ylwin';
        }
        if ($('#' + winId).window) {
            $('#' + winId).window('close');
        }
    },

    //关闭自身窗体，若自身窗体是使用showWindow方法打开的，则将window.closeArgs传至打开窗体时所传入的回调函数
    closeSelfWin: function () {
        if (window.parent != window) {
            window.parent.$.closeWindow(window.id);
        } else {
            window.close();
        }
    },

    /*
    {
    title: '窗口标题',
    winId: ID（默认为'ylDialog'）,
    title: 窗口标题（默认为'title'）,
    refresh: 显示刷新（默认是根据url来判断是否执行刷新）,
    url: '所打开的链接地址'（默认为undefined）,
    cWidth: 减少宽度值（默认为200）,
    cHeight: 减少高度值（默认为100）,
    width: 优先采用固定宽度，
    height: 优先采用固定高度
    }
    */
    showDialog: function (params) {
        if (params == undefined) {
            params = {};
        }
        if (params.cWidth == undefined) {
            params.cWidth = 200;
        }
        if (params.cHeight == undefined) {
            params.cHeight = 100;
        }
        if (params.title == undefined) {
            params.title = 'title';
        }
        if (params.winId == undefined) {
            params.winId = '__yldialog';
        }
        if (params.toolbar == undefined) {
            params.toolbar = 'div[toolbar]';
        }
        if (params.buttons == undefined) {
            params.buttons = 'div[buttons]';
        }

        if (!params.width) {
            params.width = $(document).width() - params.cWidth;
        }
        if (!params.height) {
            params.height = $(document).height() - params.cHeight;
        }

        var url = params.url;
        url = url + (url.lastIndexOf('?') == -1 ? '?' : '&') + 't=' + Math.random();

        var win = $('#' + params.winId);
        if (win.length == 0) {
            win = $('<div id="' + params.winId + '"></div>');
            win.dialog({
                title: params.title,
                collapsible: true,
                minimizable: false,
                maximizable: true,
                resizable: true,
                modal: true,
                closed: false,
                width: params.width,
                height: params.height,
                onLoad: function () {
                    win.dialog({
                        buttons: params.buttons,
                        toolbar: params.toolbar
                    });
                    $(params.toolbar + ':gt(0)', win).remove();
                    $(params.buttons, win).find('*[closeWin]').bind('click', function () {
                        win.dialog('close');
                    });
                    $(params.toolbar, win).find('*[closeWin]').bind('click', function () {
                        win.dialog('close');
                    });
                    if (params.onLoad) {
                        params.onLoad();
                    }
                }
            });
        } else {
            win.dialog('open');
        }
        if (params.refresh || win.dialog('options').url != params.url) {
            win.dialog('refresh', url);
            win.dialog({
                url: params.url
            });
        }
        return win;
    },

    //将json对象转成url参数
    //params：必需，待转换的参数对象
    //url：可选，不为空时返回参数字符串与url合并的结果
    toUrlParam: function (params, url) {
        if (params) {
            var query = '';
            if (typeof (params) == 'string') {
                query = params;
            } else {
                for (var key in params) {
                    var val = params[key];
                    if (val != undefined && val != null) {
                        query = query + encodeURIComponent(key) + '=' + encodeURIComponent(params[key]) + '&';
                    }
                }
                if (query.length > 0) {
                    query = query.substr(0, query.length - 1);
                }
            }
            if (url) {
                if (url.indexOf('?') != -1) {
                    if (url[url.length - 1] != '&') {
                        return url + '&' + query;
                    } else {
                        return url + query;
                    }
                } else {
                    return url + '?' + query;
                }
            } else {
                return query;
            }
        } else {
            return url;
        }
    },
    getEl: function (id) { return document.getElementById(id); }
});

//标准closeArgs格式
window.closeArgs = { haschange: false, message: '', data: null };






